//
//  HomePageDoctorApplyController.m
//  QuickCure1
//
//  Created by 冬冬 on 2016/11/1.
//  Copyright © 2016年 maoge. All rights reserved.
//

#import "HomePageDoctorApplyController.h"
#import "ChooseDiseaseDetailsView.h"
#import "receiveTypeController.h"
#import "complicationChooseController.h"
#import "ChooseTableController.h"
#import "NetWorkTool.h"
#import "ComplicationModel.h"
@interface HomePageDoctorApplyController ()
@property (nonatomic, weak) ChooseDiseaseDetailsView *choosDiseaseDetailsView;

//接口数据纠正
@property (nonatomic, assign) NSInteger newNum;

//存发症数据
@property (nonatomic, strong) NSArray *dataArr;
@end

@implementation HomePageDoctorApplyController

//视图显示的时候
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    if ([self.choosDiseaseDetailsView isBtnNormal]) {
        self.choosDiseaseDetailsView.treatmentAppliBtn.enabled = YES;
        self.choosDiseaseDetailsView.treatmentAppliBtn.backgroundColor = [UIColor blueColor];
    }
   
}

#pragma mark - 懒加载并发症数据数组
-(NSArray *)dataArr{
    if (!_dataArr) {
        _dataArr = [NSArray array];
    }
    return _dataArr;
}


- (void)viewDidLoad {
    [super viewDidLoad];
//    NSLog(@"%ld",self.num);
    switch (_num) {
        case 0:
            _newNum = 1;
            break;
        case 1:
            _newNum = 3;
            break;
        case 2:
            _newNum = 4;
            break;
        case 3:
            _newNum = 2;
            break;
        case 4:
            _newNum = 5;
            break;
      
    }
//导入选择并发症数据
    [self loadDataWith:_newNum];
    self.navigationItem.title = @"疾病详情选择";
    //创建一个疾病详情选择视图
    self.navigationController.navigationBar.translucent = NO;
    ChooseDiseaseDetailsView *choose = [[NSBundle mainBundle]loadNibNamed:@"ChooseDiseaseDetailsView" owner:nil options:nil].firstObject;
    choose.frame = self.view.bounds;
    
    choose.DiseaseTypesLbl.text = self.labStr;
    [self.view addSubview:choose];
    
    //关联
    self.choosDiseaseDetailsView = choose;
   
    //block传值
    [self getBlock];
    //接受通知
    [self getNotification];
}

#pragma mark - 选择并发症数据
-(void)loadDataWith:(NSInteger)dataNum{
    NSString *urlString = kComplicationListUrl;
    NSDictionary *paramDic = @{@"page":@(dataNum),@"page_size":@15,@"ci2_id":@3
                               };
    __weak HomePageDoctorApplyController *weakSelf = self;
    [[NetWorkTool shareManager] POST_RequestWithUrlString:urlString parmas:paramDic successBloock:^(NSDictionary* responseObject) {
        
//        NSLog(@"responseObject = %@",responseObject);
        NSArray *arr = responseObject[@"data"];
      weakSelf.dataArr = [NSArray yy_modelArrayWithClass:[ComplicationModel class] json:arr];
//        NSLog(@"%@",self.dataArr);
        
    } failureBlock:^(NSError *error) {
        
        NSLog(@"error = %@",error);
    }];
}

#pragma mark-block传值
-(void)getBlock{
    __weak HomePageDoctorApplyController *weakVC = self;
    //治疗方式block传值
   
    self.choosDiseaseDetailsView.receiveType = ^(){
         receiveTypeController *receiveTypeVC = [[receiveTypeController alloc]init];
        [weakVC.navigationController pushViewController:receiveTypeVC animated:YES];
    };
    
    
   
    self.choosDiseaseDetailsView.complicationBlock = ^(){
        //选择并发症Block
       
        complicationChooseController *complicationChooseVC = [[complicationChooseController alloc]init];
        complicationChooseVC.dataNum = self.newNum;
        
        [weakVC.navigationController pushViewController:complicationChooseVC animated:YES];
        complicationChooseVC.dataArr = weakVC.dataArr;
//         NSLog(@"%@",self.dataArr);
    };
    
    
    self.choosDiseaseDetailsView.DiseaseSubdivisionBlock = ^(){
        //疾病细分Block
       ChooseTableController *DiseaseSubdivisionVC = [[ChooseTableController alloc]init];
        [DiseaseSubdivisionVC LoadDataWith:(int)_newNum Page:15];
        [weakVC.navigationController pushViewController:DiseaseSubdivisionVC animated:YES];

    };
}

#pragma maek -接受通知
-(void)getNotification{
   //治疗方式通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(clickReceiveType:) name:@"receiveType" object:nil];
    
    //并发症选择通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(clickcomplicationChoose:) name:@"complicationChoose" object:nil];
    
    //疾病细分通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(clickDiseaseSubdivision:) name:@"DiseaseSubdivision" object:nil];
}

#pragma mark - 通知的实现方法
//治疗方式
-(void)clickReceiveType:(NSNotification *)sender{
    
//  NSLog(@"%@",sender.object);
    UITableViewCell *cell = sender.object;
    self.choosDiseaseDetailsView.ChooseTreatmentLbl.text = cell.textLabel.text;
    self.choosDiseaseDetailsView.ChooseTreatmentLbl.textColor = [UIColor blackColor];
}


//并发症选择
-(void)clickcomplicationChoose:(NSNotification *)sender{
    NSMutableArray *array = sender.object;
    self.choosDiseaseDetailsView.ComplicationLbl.textColor = [UIColor blackColor];
    
    NSMutableString *MStr = [NSMutableString string];
    for (NSString *str in array) {
        MStr = [MStr stringByAppendingFormat:@" %@",str].mutableCopy;
    }
//    NSLog(@"%@",MStr);
    if (array.count) {
        self.choosDiseaseDetailsView.ComplicationLbl.text = MStr;
    }else{
        self.choosDiseaseDetailsView.ComplicationLbl.textColor = [UIColor lightGrayColor];
    }
   
}

//疾病细分选择
-(void)clickDiseaseSubdivision:(NSNotification *)sender{
    NSString *str = sender.object;
    self.choosDiseaseDetailsView.DiseaseSubdivisionLbl.text = str;
    self.choosDiseaseDetailsView.DiseaseSubdivisionLbl.textColor = [UIColor blackColor];
}
//移除通知
-(void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];

}
@end
